package com.css.fxfzcpxx.modules.typeManage.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.css.fxfzcpxx.base.response.RestResponse;
import com.css.fxfzcpxx.constants.YNEnum;
import com.css.fxfzcpxx.modules.courseManage.entity.CourseManage;
import com.css.fxfzcpxx.modules.informationDelivery.entity.InfDelivery;
import com.css.fxfzcpxx.modules.questionData.entity.QuestionData;
import com.css.fxfzcpxx.modules.typeManage.entity.TypeManage;
import com.css.fxfzcpxx.modules.typeManage.param.TypeManageParam;
import com.css.fxfzcpxx.modules.typeManage.repository.TypeManageRepository;
import com.css.fxfzcpxx.modules.typeManage.service.TypeManageService;
import com.css.fxfzcpxx.util.UUIDGenerator;
import com.css.fxfzcpxx.web.PlatformSessionContext;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @Author: zhangSongRui
 * @create_time: 2023/03/08
 * @Description: 类别管理Service
 */
@Service
public class TypeManageServiceImpl implements TypeManageService {

    @Resource
    private TypeManageRepository typeManageRepository;

    @Override
    public List<TypeManage> getType(String plate) {
        return typeManageRepository.getType(plate);
    }

    @Override
    public RestResponse save(TypeManage typeManage) {
        //根据类型板块查重
        TypeManage name=typeManageRepository.queryByPlateAndName(typeManage.getTypePlate(),typeManage.getTypeName().trim());
        if (null!=name){
            return RestResponse.fail("类别名称重复");
        }
        typeManage.setId(UUIDGenerator.getUUID());
        typeManage.setCreateTime(new Date());
        typeManage.setCreateUser(PlatformSessionContext.getUserID());
        typeManage.setDelFlag(YNEnum.N.toString());
        typeManageRepository.save(typeManage);
        return RestResponse.succeed("新增成功");
    }

    @Override
    public RestResponse delete(String id, String typePlate) {
        int del = 1;
        //查询该类别是否绑定了数据
        if (typePlate.equals("技术资料") | typePlate.equals("法规标准") | typePlate.equals("科普知识") | typePlate.equals("典型案例")) {
            List<InfDelivery> information = typeManageRepository.queryDataDelivery(id);
            del = information.size();
        } else if (typePlate.equals("在线课堂")) {
            List<CourseManage> courseManages = typeManageRepository.queryDataCourse(id);
            del = courseManages.size();
        } else if (typePlate.equals("题库")) {
            List<QuestionData> questionData = typeManageRepository.queryDataQuestion(id);
            del = questionData.size();
        }
        //如果没数据则直接删除
        if (del == 0) {
            typeManageRepository.delete(id);
            return RestResponse.succeed();
        } else {
            return RestResponse.succeed("请先移除此类别下的数据，否则无法删除");
        }
    }

    @Override
    public void update(TypeManage typeManage) {
        typeManage.setUpdateTime(new Date());
        typeManage.setUpdateUser(PlatformSessionContext.getUserID());
        typeManageRepository.update(typeManage);
    }

    @Override
    public TypeManage queryIdByName(String name) {
        return typeManageRepository.queryIdByName(name);
    }

    @Override
    public RestResponse getListByParam(TypeManageParam param) {
        Map<String, Object> map = new HashMap<>();
        List<String> list=null;
        if (!StringUtils.isEmpty(param.getTypeId())){
            list = Arrays.asList(param.getTypeId().split(","));
        }
        if (param.getTypePlate().equals("技术资料") | param.getTypePlate().equals("法规标准") | param.getTypePlate().equals("科普知识") | param.getTypePlate().equals("典型案例")) {
            map = typeManageRepository.getPage(param,
                    "select id,means_name as name,create_time from cpxx_information_delivery",
                    "means_name", "means_type_id", list);
        } else if (param.getTypePlate().equals("在线课堂")) {
            map = typeManageRepository.getPage(param,
                    "select id,course_name as name,create_time from cpxx_course_manage",
                    "course_name", "course_type_id", list);
        } else if (param.getTypePlate().equals("题库")) {
            map = typeManageRepository.getPage(param,
                    "select id,question_name as name,create_time from cpxx_question_data",
                    "question_name", "type_id", list);
        }
        return RestResponse.succeed(map);
    }

    @Override
    public void transferByIds(String typePlate, String typeId, String ids) {
        if (typePlate.equals("技术资料") | typePlate.equals("法规标准") | typePlate.equals("科普知识") | typePlate.equals("典型案例")) {
            String s="知识学习-"+typePlate;
            if (typePlate.equals("典型案例")){
                s="服务案例-科普知识";
            }
            typeManageRepository.transferByIdsALm(s,"means_type_id", typeId, Arrays.asList(ids.split(",")));
        } else if (typePlate.equals("在线课堂")) {
            typeManageRepository.transferByIds("cpxx_course_manage","course_type_id", typeId, Arrays.asList(ids.split(",")));
        } else if (typePlate.equals("题库")) {
            typeManageRepository.transferByIds("cpxx_question_data","type_id", typeId, Arrays.asList(ids.split(",")));
        }
    }

    @Override
    public void transferByTypeId(String typePlate, String oldTypeIds, String newTypeId) {
        if (typePlate.equals("技术资料") | typePlate.equals("法规标准") | typePlate.equals("科普知识") | typePlate.equals("典型案例")) {
            String s="知识学习-"+typePlate;
            if (typePlate.equals("典型案例")){
                s="服务案例-科普知识";
            }
            typeManageRepository.transferByTypeIdALm(s,"means_type_id", newTypeId, Arrays.asList(oldTypeIds.split(",")));
        } else if (typePlate.equals("在线课堂")) {
            typeManageRepository.transferByTypeId("cpxx_course_manage","course_type_id", newTypeId, Arrays.asList(oldTypeIds.split(",")));
        } else if (typePlate.equals("题库")) {
            typeManageRepository.transferByTypeId("cpxx_question_data","type_id", newTypeId, Arrays.asList(oldTypeIds.split(",")));
        }
    }

    @Override
    public TypeManage findById(String id) {
        return typeManageRepository.queryById(id);
    }

}
